﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using DevExpress.XtraEditors.DXErrorProvider;
using ChinaDynastyTours.Models;
using System.Threading;

namespace ChinaDynastyTours.Views
{
    public partial class TeamGroupExpenseAddForm : DevExpress.XtraEditors.XtraForm
    {
        //private ProviderTypeEnum _ExpenseTypeBlong;
        private string _TourGroupID;
        private int _TourGroupScheduleID;

        public TeamGroupExpenseAddForm(string tourGroupId, int tourGroupScheduleId)
        {
            InitializeComponent();

            _TourGroupID = tourGroupId;
            _TourGroupScheduleID = tourGroupScheduleId;

        }

        private void TeamGroupExpenseAddForm_Load(object sender, EventArgs e)
        {

            tbExpenseType.Properties.DataSource = BaseInfoCtrl.GetExpenseTypeData(1);

            tbExpenseType.ItemIndex = 0;
            InitValidationRules();

        }

        /// <summary>
        /// 供应商选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProviderNameEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            ProviderSelectedToolForm providerForm = new ProviderSelectedToolForm();
            providerForm._ProviderTypes = new ProviderTypeEnum[] { ProviderTypeEnum.宾馆, ProviderTypeEnum.餐厅, ProviderTypeEnum.飞机票, ProviderTypeEnum.火车票, ProviderTypeEnum.景点, ProviderTypeEnum.旅行社, ProviderTypeEnum.汽车, ProviderTypeEnum.演出 };
            if (providerForm.ShowDialog(this) == DialogResult.OK)
            {
                DevExpress.XtraEditors.ButtonEdit btnEdit = sender as DevExpress.XtraEditors.ButtonEdit;
                btnEdit.Tag = providerForm.ReturnProviderID;
                btnEdit.EditValue = providerForm.ReturnProviderName;
            }
        }

        /// <summary>
        /// 控件数据校验
        /// </summary>
        private void InitValidationRules()
        {
            // <notEmptyTextEdit> 
            ConditionValidationRule notEmptyValidationRule = new ConditionValidationRule();
            notEmptyValidationRule.ConditionOperator = ConditionOperator.IsNotBlank;
            notEmptyValidationRule.ErrorText = "不能为空";
            // </notEmptyTextEdit>

            ConditionValidationRule notZeroRule = new ConditionValidationRule();
            notZeroRule.ConditionOperator = ConditionOperator.NotEquals;
            notZeroRule.Value1 = 0;
            notZeroRule.ErrorText = "不能为0";


            dxValidationProvider.SetValidationRule(tbEtcServiceName, notEmptyValidationRule);
            dxValidationProvider.SetValidationRule(tbEtcProviderName, notEmptyValidationRule);
            dxValidationProvider.SetValidationRule(tbEtcBudget, notZeroRule);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!dxValidationProvider.Validate()) return;

            ExpenseEntity expense = new ExpenseEntity();
            expense.AdjustAmount = 0;
            expense.ApplyPayTotal = 0;
            expense.PayTotal = 0;
            expense.Status = ExpenseStatusEnum.计划.ToString();
            expense.PayStatus = PayStatusEnum.未付.ToString();
            expense.TourgroupID = _TourGroupID;
            expense.TourgroupName = TeamGroupCtrl.GetTeamGroupNameById(_TourGroupID);
            expense.TourGroupScheduleID = _TourGroupScheduleID;
            expense.CreateByUserName = Thread.CurrentPrincipal.Identity.Name;
            expense.CreateTime = DateTime.Now.ToString("yyyy-MM-dd");

            expense.Budget = decimal.Parse(tbEtcBudget.EditValue.ToString());
            expense.ProviderID = tbEtcProviderName.Tag == null ? -1 : int.Parse(tbEtcProviderName.Tag.ToString());
            expense.ProviderName = tbEtcProviderName.Text == null ? "" : tbEtcProviderName.Text;
            expense.ServiceName = tbEtcServiceName.Text;
            expense.TypeBelong = ProviderTypeEnum.其它.ToString();
            expense.ExpenseTypeID = int.Parse(tbExpenseType.EditValue.ToString());//所属费用类型(费用类型表中存储着编号，此编号是固定的，1房费、2团费、3餐费、4交通费、5门票、6杂费)
            expense.ExpenseType = tbExpenseType.Text;//同上
            expense.Remark = "";

            ExpensesCtrl.CreateExpenseData(expense);

            this.DialogResult = DialogResult.OK;
        }
    }
}
